package com.zklh.leetcode.other.problem35;


/**
 * 使用二分查找,实现
 */
public class Solution {
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length-1;
        int mid = 0;
        /* 当两个不相邻就一直找 */
        while(left < right-1){
            mid = (left+right) / 2;
            if(nums[mid] == target){
                return mid;
            }else if(nums[mid] > target){
                right = mid-1;
            }else{
                left = mid+1;
            }
        }
        return left;
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.searchInsert(new int[]{1, 3, 6, 8, 12}, 13));
    }


}
